#include "includes.h"
#include "./wrap.h"
void fn(char *p);
int main(int argc ,char** argv)
{
    int serversocket  =  Socket(AF_INET,SOCK_STREAM,0);
    char buf[BUFSIZ];
    struct sockaddr_in  addr ;
    if(argc !=2){
        printf("usage %s port(num).\n",argv[0]);
        exit(-1);
    }
    memset(&addr,0,sizeof(addr));
    addr.sin_family =AF_INET;
    addr.sin_port = htons(atoi(argv[1]));
    int r=  inet_pton(AF_INET,"127.0.0.1",&(addr.sin_addr.s_addr));
    if(r ==0){
        sys_error("inet_pton");
    }
    Connect(serversocket,(struct sockaddr*)&addr,sizeof(addr));

    while(1){
        CLEAR_ARRAY(buf);
        r= Read(STDIN_FILENO,buf,sizeof(buf));
        if(0 == strncmp("quit",buf,4)){
            break;
        }
        else{
            Write(serversocket,buf,r);
        }
        CLEAR_ARRAY(buf);
        r= Read(serversocket,buf,sizeof(buf));
        Write(STDOUT_FILENO,buf,r);
    }
    
    Close(serversocket);
    return 0;
}
void fn(char *p)
{
    if(!p){
        return;
    }
    while (*p)
    {
        *p =toupper(*p);
        p++;
    }
    
}
